package com.framework.common.config;


import com.framework.common.jdbc.interceptor.MysqlPagingInterceptor;
import com.framework.common.jdbc.interceptor.TimerInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/4/11 21:34
 * @since JDK 1.8
 */
public class MybatisConfig {

    @Resource
    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.framework.service.core.dao.model");//每一张表对应的实体类
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("mybatis/*/*.xml"));
        bean.setPlugins(new Interceptor[]{getMysqlPagingInterceptor()});
        return bean.getObject();
    }

//	//配置mybatis的分页插件pageHelper
//    @Bean
//    public PageHelper pageHelper(){
//        PageHelper pageHelper = new PageHelper();
//        Properties properties = new Properties();
//        properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
//        properties.setProperty("offsetAsPageNum","true");
//        properties.setProperty("rowBoundsWithCount","true");
//        properties.setProperty("reasonable","true");
//        properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
//        properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
//        pageHelper.setProperties(properties);
//        return pageHelper;
//    }

    @Bean
    public Interceptor  getMysqlPagingInterceptor() {
        return new MysqlPagingInterceptor();
    }

    @Bean
    public Interceptor  getTimerInterceptor() {
        return new TimerInterceptor();
    }
}
